package com.ss.android.common.app;

import android.app.Activity;

import com.bytedance.common.utility.collection.WeakList;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public final class ActivityStackManager {
    public interface StackRecorder {
        String getRecorderKey();

        boolean isFinishing();
    }

    private static Set<String> sActivitySet = new HashSet();
    private static WeakList<StackRecorder> sFinishedActivityContainer = new WeakList();
    private static volatile int sActivityId;

    public static String buildKey(Activity activity) {
        StringBuilder stringBuilder = new StringBuilder().append(activity.getClass().getCanonicalName()).append("@");
        int activityId = ActivityStackManager.sActivityId;
        ActivityStackManager.sActivityId = activityId + 1;
        return stringBuilder.append(activityId).toString();
    }

    public static void addRecorder(StackRecorder stackRecorder) {
        if (stackRecorder != null) {
            try {
                ActivityStackManager.sFinishedActivityContainer.add(stackRecorder);
                ActivityStackManager.sActivitySet.add(stackRecorder.getRecorderKey());
            } catch (Throwable throwable) {
            }
        }
    }

    public static String getAliveActivitiesString() {
        String v0_2;
        if (ActivityStackManager.sActivitySet != null && !ActivityStackManager.sActivitySet.isEmpty()) {
            try {
                StringBuilder v2 = new StringBuilder();
                Iterator v3 = ActivityStackManager.sActivitySet.iterator();
                int v1;
                for (v1 = 0; v3.hasNext(); ++v1) {
                    Object v0_1 = v3.next();
                    if (v1 < ActivityStackManager.sActivitySet.size() - 1) {
                        v2.append(((String) v0_1)).append("|");
                    } else {
                        v2.append(((String) v0_1));
                    }
                }

                v0_2 = v2.toString();
            } catch (Throwable v0) {
                v0_2 = "";
            }
        } else {
            v0_2 = "";
        }

        return v0_2;
    }

    public static void removeRecorder(StackRecorder stackRecorder) {
        if (stackRecorder != null) {
            try {
                ActivityStackManager.sActivitySet.remove(stackRecorder.getRecorderKey());
            } catch (Throwable throwable) {
            }
        }
    }

    public static String getFinishedActivitiesString() {
        String v0_2;
        if (ActivityStackManager.sFinishedActivityContainer != null && !ActivityStackManager.sFinishedActivityContainer.weakHashMap.isEmpty()) {
            try {
                StringBuilder v2 = new StringBuilder();
                Iterator v3 = ActivityStackManager.sFinishedActivityContainer.iterator();
                int v1;
                for (v1 = 0; v3.hasNext(); ++v1) {
                    Object v0_1 = v3.next();
                    if (v0_1 != null && !ActivityStackManager.sActivitySet.contains(((StackRecorder) v0_1).getRecorderKey()) && (((StackRecorder) v0_1).isFinishing())) {
                        if (v1 < ActivityStackManager.sFinishedActivityContainer.weakHashMap.size() - 1) {
                            v2.append(((StackRecorder) v0_1).getRecorderKey()).append("|");
                        } else {
                            v2.append(((StackRecorder) v0_1).getRecorderKey());
                        }
                    }
                }

                v0_2 = v2.toString();
            } catch (Throwable v0) {
                v0_2 = "";
            }
        } else {
            v0_2 = "";
        }

        return v0_2;
    }
}

